package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.ILogMapper;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用户实现类
 */

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private ILogMapper logMapper;
    @Autowired
    private IGradeMapper gradeMapper;
    @Autowired
    private IRoleMapper roleMapper;
    @Override
    public UserBean login(String userName) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("u_userName",userName);
            UserBean loginUser=userMapper.selectOne(qw);
            loginUser.setRoleList(roleMapper.findRoleByUserId(loginUser.getUserId()));
            loginUser.setGradeList(gradeMapper.findGradeByLoginUserId(loginUser.getUserId()));
            return loginUser;
    }

    @Override
    public void updatePwd(Integer loginUserId, String newPwd) {
            UserBean userObj=userMapper.selectById(loginUserId);
        Md5Hash md5Hash=new Md5Hash(newPwd,userObj.getSalt(),1024);
        userObj.setPwd(md5Hash.toHex());
            userMapper.updateById(userObj);
    }

    @Override
    public IPage<UserBean> cutByItem(Integer pageNO, String userName) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        if (userName !=null && userName.length() !=0){
            qw.like("u_userName",userName);
        }
        return userMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public UserBean findByUserName(String userName) {
        QueryWrapper<UserBean> qw=new QueryWrapper<>();
        qw.eq("u_userName",userName);
        UserBean userBean=userMapper.selectOne(qw);
        if (userBean !=null) {
            userBean.setRoleList(roleMapper.findRoleByUserId(userBean.getUserId()));
            userBean.setGradeList(gradeMapper.findGradeByLoginUserId(userBean.getUserId()));
        }
        return userBean;
    }

    @Override
    public void add(UserBean user, Integer[] roleId) {
        user.setSalt(this.getSalt());
        Md5Hash md5Hash=new Md5Hash(user.getPwd(),user.getSalt(),1024);
        user.setPwd(md5Hash.toHex());
        userMapper.insert(user);
      userMapper.addRole(user.getUserId(),roleId);



    }

    @Override
    public void del(Integer userId) {
       userMapper.del(userId);
       userMapper.deleteById(userId);
    }

    @Override
    public UserBean findById(Integer userId) {
        UserBean userObj=userMapper.selectById(userId);
        userObj.setRoleList(roleMapper.findRoleByUserId(userId));
        userObj.setGradeList(gradeMapper.findGradeByLoginUserId(userId));
        return userObj;
    }

    @Override
    public void updateRole(Integer userId, Integer[] newRoleId) {
        userMapper.updateRole(userId,newRoleId);
    }

    private String getSalt(){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()";
        StringBuffer sb=new StringBuffer();
        for (int i=0;i<6;i++){
            int index= (int) (Math.random()*str.length());
            sb.append(str.charAt(index));
        }
        return sb.toString();
    }
}
